package com.golden.procedure.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.procedure.dao.WorkDesignOrderMapper;
import com.golden.procedure.dao.WorkOrderPickMapper;
import com.golden.procedure.domain.WorkCommentDO;
import com.golden.procedure.domain.WorkDesignOrderDO;
import com.golden.procedure.domain.WorkOrderDO;
import com.golden.procedure.entity.WorkDesignOrder;
import com.golden.procedure.entity.WorkDesignOrderExample;
import com.golden.procedure.entity.WorkOrderPick;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.ResultSupport;
import com.golden.procedure.service.IWorkCommentService;
import com.golden.procedure.service.IWorkDesignOrderService;
import com.golden.procedure.service.IWorkOrderPickService;
import com.golden.procedure.service.IWorkOrderService;
import com.golden.procedure.service.IWorkTaskHistoryService;
import com.golden.procedure.util.BeanUtilsExtends;
import com.golden.procedure.util.BonConstant;
import com.golden.procedure.util.TaskDefKey;

@Service("workDesignOrderService")
public class WorkDesignOrderService extends BaseService implements IWorkDesignOrderService {

	@Autowired
	WorkDesignOrderMapper workDesignOrderMapper;
	@Autowired
	WorkOrderPickMapper workOrderPickMapper;
	@Autowired
	IWorkOrderService workOrderService;
	@Autowired
	IWorkOrderPickService workOrderPickService;
	@Autowired
	IWorkCommentService workCommentService;
	@Autowired
	IWorkTaskHistoryService workTaskHistoryService;

	/**
	 * 添加设计制作单<b>Description: 添加设计制作单</b><br>
	 * 
	 * @param domain
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月20日 下午3:43:42 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO add(WorkDesignOrderDO domain) {
		ResultDO result = new ResultSupport();
		if (isNumberInValid(domain.getWorkOrderPickID())) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("参数错误");
			return result;
		}

		// 1. 查询选片记录
		WorkOrderPick pick = workOrderPickMapper.selectByPrimaryKey(domain.getWorkOrderPickID());
		if(null == pick) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("选片记录查询失败");
			return result;
		}
		// 选片完成后，才能发送制作单
		if (pick.getStatus() != BonConstant.PICK_STATUS_OK) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("选片任务还未结束");
			return result;
		}

		// 2.查询工单
		ResultDO resultDO = workOrderService.get(pick.getWorkOrderID());
		if (!resultDO.isSuccess()) {
			return resultDO;
		}
		WorkOrderDO orderDO = (WorkOrderDO) resultDO.getModel(ResultDO.FIRST_MODEL_KEY);

		// 3.查询业务流水号
		String businessNo = workOrderPickService.getBusinessNo(domain.getWorkOrderPickID());
		if (null == businessNo) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("选片信息查询失败");
			return result;
		}
		// 4.写入设计制作表数据
		WorkDesignOrder entity = new WorkDesignOrder();
		if (!BeanUtilsExtends.copyProperties(entity, domain)) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("数据格式转换错误");
			return result;
		}

		entity.setWorkOrderID(orderDO.getId());
		entity.setProcessInstanceID(orderDO.getProcessInstanceID());
		entity.setOrderID(orderDO.getOrderID());
		entity.setOrderProductSceenID(orderDO.getOrderProductSceenID());
		entity.setBusinessNo(businessNo);

		entity.setPhotoNum(domain.getPhotoNum());
		entity.setProductNum(domain.getProductNum());
		entity.setCheckDesignDate(new Date(Long.parseLong(domain.getCheckDesignDateStr())));
		entity.setDeliveryDate(new Date(Long.parseLong(domain.getDeliveryDateStr())));
		entity.setStatus(BonConstant.STATUS_TRUE);
		entity.setIsDelete(BonConstant.DB_NORMAL);
		entity.setCreatetime(Calendar.getInstance().getTime());

		int ret = workDesignOrderMapper.insert(entity);
		if (ret < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("发送失败");
			result.setSuccess(false);
		}
		
		// 写入服务记录
		WorkCommentDO commentDO = new WorkCommentDO();
		commentDO.setWorkOrderID(entity.getWorkOrderID());
		commentDO.setProcessInstanceID(entity.getProcessInstanceID());
		commentDO.setTaskDefID(TaskDefKey.SELECT_PHOTO.getKey()); // 选片
		commentDO.setTaskName(TaskDefKey.SELECT_PHOTO.getName());
		commentDO.setTaskID(pick.getTaskID()); // 从acitivti获取
		commentDO.setType(BonConstant.COMMENT_TYPE_EVENT); // 0备注；1事件
		commentDO.setRemark("提交设计制作单：" + domain.getComment()); // 0普通；1特殊
		commentDO.setFlag(domain.getIsUrgent()); // 0普通；1加急
		commentDO.setAuthorID(domain.getAuthorID());
		commentDO.setAuthor(domain.getAuthor());
		workCommentService.add(commentDO);
		
		return result;
	}

	@Override
	public ResultDO checkSend(Integer workOrderPickID) {

		ResultDO result = new ResultSupport();
		if (isNumberInValid(workOrderPickID)) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("参数错误");
			return result;
		}

		String businessNo = workOrderPickService.getBusinessNo(workOrderPickID);
		if (null == businessNo) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("选片记录查询失败");
			return result;
		}

		WorkDesignOrderExample example = new WorkDesignOrderExample();
		WorkDesignOrderExample.Criteria criteria = example.createCriteria();
		criteria.andBusinessNoEqualTo(businessNo);
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);
		example.setOrderByClause(" id desc ");

		List<WorkDesignOrder> list = workDesignOrderMapper.selectByExample(example);
		WorkDesignOrderDO designOrderDO = new WorkDesignOrderDO();
		if (null != list && list.size() > 0) {
			designOrderDO = getWorkDesignOrderDO(list.get(0));
		} else {
			designOrderDO.setStatus(BonConstant.STATUS_FALSE);
			designOrderDO.setBusinessNo(businessNo);
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, designOrderDO);
		return result;
	}

}
